clf
clc
clear

piG = 0;
piB = -1;
trueMu = 0.5;
perceivedMu = trueMu;

mu = perceivedMu;
lossAversion = 1;

gridpoints = 300;
m = linspace(-piG, -piB, gridpoints);

numLambda = 2;
lambda = linspace(0.1, 0.3, numLambda);
p0 = zeros(gridpoints,numel(lambda));
pG0 = zeros(gridpoints,numel(lambda));
pB0 = zeros(gridpoints,numel(lambda));
postAcc = zeros(gridpoints,numel(lambda));



for i = 1 : gridpoints
    for j = 1 : numel(lambda)
        [p0(i, j), pG0(i, j), pB0(i, j)] = binaryModel(m(i) , piG, piB, mu, lambda(j), lossAversion);
        p0(i, j) = min(1, max(0, p0(i, j))); 
        pG0(i, j) = min(1, max(0, pG0(i, j)));
        postAcc(i,j) = mu * pG0(i, j) / (mu * pG0(i, j) + (1 - mu) * pB0(i, j));
    end
end





trueP =min(1, max(0, ((trueMu / mu) * (1 - p0) - (1 - pG0))./( (1 - p0) ./ p0 .* pG0 - (1 - pG0) )));

p0 = trueP;

for i = 2 : gridpoints
    p0(i,1) = max(p0(i,1), p0(i-1,1));
    p0(i,2) = max(p0(i,2), p0(i-1,2));
end


l1 = num2str(mean(lambda(1:numLambda/2)));
l2 = num2str(mean(lambda(numLambda/2+1: numLambda)));


fracHighCost = p0(:,2) ./ (p0(:,1) + p0(:,2));

p0(:,1) = min(1, max(0, p0(:,1)));
p0(:,2) = min(1, max(0, p0(:,2)));
fracHighCost = min(1.5, max(0, fracHighCost));

if 1  
    figure('Color',[1, 1, 1])
    hold on
    plot(m, fracHighCost(:), 'k', 'LineWidth',8);
    plot(m, p0(:,1), 'b', 'LineWidth',4);
    plot(m, p0(:,2), 'r--', 'LineWidth',4);
    plot(m, fracHighCost(:), 'k', 'LineWidth',8);
    set(gca,'fontsize', 25);
    ylabel('Participation probability')
    xlabel('Incentive m')
    axis([0 1 0 1])
    legend('P(high cost|particip.)', strcat('\lambda = ', l1, ' (low cost)'),strcat('\lambda = ', l2, ' (high cost)'),'Location','southeast')
    hold off

    filename='../graphs/mainGraph.pdf';
    saveas(gcf,filename); 

end




